/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.jinatonic.confetti.sample;

import com.github.jinatonic.confetti.*;
import com.github.jinatonic.confetti.confetto.BitmapConfetto;
import com.github.jinatonic.confetti.confetto.Confetto;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class FallingConfettiWithListenerAbility extends Ability implements Component.ClickedListener, ConfettoGenerator, ConfettiManager.ConfettiAnimationListener {
    private int numConfettiOnScreen;
    protected ComponentContainer container;

    protected int goldDark, goldMed, gold, goldLight;

    private final List<ConfettiManager> activeConfettiManagers = new ArrayList<>();

    private int size;
    private int velocitySlow, velocityNormal;

    private PixelMap bitmap;

    private Text tvNumber;

    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_confetti2);
        getWindow().setStatusBarColor(Color.getIntColor("#946335"));
        container = (ComponentContainer) findComponentById(ResourceTable.Id_container);
        tvNumber = (Text) findComponentById(ResourceTable.Id_tv_number);
        findComponentById(ResourceTable.Id_generate_confetti_once_btn).setClickedListener(this);
        findComponentById(ResourceTable.Id_generate_confetti_stream_btn).setClickedListener(this);
        findComponentById(ResourceTable.Id_generate_confetti_infinite_btn).setClickedListener(this);
        findComponentById(ResourceTable.Id_generate_confetti_stop_btn).setClickedListener(this);
        findComponentById(ResourceTable.Id_back).setClickedListener(this);
        goldDark = Color.getIntColor("#946335");
        goldMed = Color.getIntColor("#ab804b");
        gold = Color.getIntColor("#cea774");
        goldLight = Color.getIntColor("#edd3b2");
        size = AttrHelper.vp2px(6, container.getContext());
        velocitySlow = AttrHelper.vp2px(50, container.getContext());
        velocityNormal = AttrHelper.vp2px(100, container.getContext());
        Resource imageResource = null;
        try {
            imageResource = getResourceManager().getResource(ResourceTable.Media_snowflake);
            bitmap = ImageSource.create(imageResource, null).createPixelmap(null);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        }

    }


    public void onActive() {
        super.onActive();
    }


    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_generate_confetti_once_btn:
                activeConfettiManagers.add(generateOnce());
                break;
            case ResourceTable.Id_generate_confetti_stream_btn:
                activeConfettiManagers.add(generateStream());
                break;
            case ResourceTable.Id_generate_confetti_infinite_btn:
                activeConfettiManagers.add(generateInfinite());
                break;
            case ResourceTable.Id_generate_confetti_stop_btn:
                for (ConfettiManager confettiManager : activeConfettiManagers) {
                    confettiManager.terminate();
                }
                activeConfettiManagers.clear();
                break;
            case ResourceTable.Id_back:
                terminateAbility();
                break;
        }
    }


    protected ConfettiManager generateOnce() {
        return getConfettiManager().setNumInitialCount(20)
                .setEmissionDuration(0)
                .setConfettiAnimationListener(this)
                .animate();
    }


    protected ConfettiManager generateStream() {
        return getConfettiManager().setNumInitialCount(0)
                .setEmissionDuration(3000)
                .setEmissionRate(20)
                .setConfettiAnimationListener(this)
                .animate();
    }


    protected ConfettiManager generateInfinite() {
        return getConfettiManager().setNumInitialCount(0)
                .setEmissionDuration(ConfettiManager.INFINITE_DURATION)
                .setEmissionRate(20)
                .setConfettiAnimationListener(this)
                .animate();
    }

    private ConfettiManager getConfettiManager() {
        final ConfettiSource source = new ConfettiSource(0, -size, container.getWidth(), -size);
        return new ConfettiManager(this, this, source, container)
                .setVelocityX(0, velocitySlow)
                .setVelocityY(velocityNormal, velocitySlow)
                .setRotationalVelocity(180, 90)
                .setTouchEnabled(true);
    }


    public Confetto generateConfetto(Random random) {
        return new BitmapConfetto(bitmap);
    }


    public void onAnimationStart(ConfettiManager confettiManager) {
        MyToast.show(getContext(), "Starting confetti animation",
                MyToast.ToastLayout.BOTTOM);
    }


    public void onAnimationEnd(ConfettiManager confettiManager) {
        numConfettiOnScreen = 0;
        updateNumConfettiTxt();
        MyToast.show(getContext(), "Ending confetti animation",
                MyToast.ToastLayout.BOTTOM);
    }


    public void onConfettoEnter(Confetto confetto) {
        numConfettiOnScreen++;
        updateNumConfettiTxt();
    }


    public void onConfettoExit(Confetto confetto) {
        numConfettiOnScreen--;
        updateNumConfettiTxt();
    }

    private void updateNumConfettiTxt() {
        tvNumber.setText("Active confetti count:" + numConfettiOnScreen);
    }

    @Override
    protected void onBackPressed() {
        super.onBackPressed();

        for (ConfettiManager confettiManager : activeConfettiManagers) {
            confettiManager.terminate();
        }
        activeConfettiManagers.clear();
    }
}
